package com.mapbased.wfw.handler;

import java.util.*;
import javax.servlet.http.*;

import org.dom4j.*;
import com.mapbased.wfw.*;

public class LocaleHandler
    extends PageHandler
{
    public LocaleHandler()
    {
        super();
    }

    /**
     * defaultAction
     *
     * @param context RenderContext
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */

    public Object localeBinding(java.util.Map m)
    {
        Object o=m.get("locale");
        if(o instanceof String[])
        {
            o=((String[])o)[0];
        }
        if(o == null)
        {
            o="";
        }
        final Locale[] ls=java.util.Locale.getAvailableLocales();
        if(o.equals(""))
        {

            return ls;
        }
        final Locale l=buildLocale(o.toString());

        Object[] litems=new Object[ls.length];
        for(int i=0;i < ls.length;i++)
        {

            litems[i]=new LocalItem(l,ls[i]);
        }
        return litems;

    }

    public static Locale buildLocale(String temp)
    {
        //  Loacle locale;

        int pos=temp.indexOf('_');
        if(pos == -1)
        {
            return new Locale(temp,"","");

        }

        String language=temp.substring(0,pos);
        temp=temp.substring(pos + 1);
        pos=temp.indexOf('_');
        if(pos == -1)
        {
            return new Locale(language,temp,"");

        }

        String country=temp.substring(0,pos);
        temp=temp.substring(pos + 1);

        return new Locale(language,country,temp);

    }

    public Object selectAction(RenderContext ctx)
    {
        String l=ctx.getSureSingleParam("locale");
        Cookie ck=new Cookie("locale",l);
        ck.setDomain(this.domain);
        ck.setSecure(false);
        ck.setMaxAge(365 * 24 * 60 * 60000);
        ck.setPath("/");
        ctx.getResponse().addCookie(ck);
        ctx.setLocale(l);
        ctx.getRequestMap().put("action",null);
        return "/pages/local"+ ctx.getPageExtName();

    }

    public Object defaultAction(RenderContext ctx)
    {
        ctx.getRequestMap().put("locale",ctx.getLocale());
        return null;
    }

    /**
     *
     * @param ele Element
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */
    protected void fetchAttributs(Element ele)
    {
        Attribute a=ele.attribute("domain");
        if(a != null)
        {
            this.domain=a.getValue();
            ele.remove(a);
        }

    }

    String domain=".mapbased.com";
    public static class LocalItem
        implements Keep
    {
        Locale l;
        Locale tofetch;
        public LocalItem(Locale l,Locale tofetch)
        {
            this.l=l;
            this.tofetch=tofetch;
        }

        public String getDisplayCountry()
        {
            return tofetch.getDisplayCountry(l);
        }

        public String getDisplayLanguage()
        {
            return tofetch.getDisplayLanguage(l);
        }

        public String getDisplayVariant()
        {
            return tofetch.getDisplayVariant(l);
        }

        public String getDisplayName()
        {
            return tofetch.getDisplayName(l);
        }

        public String toString()
        {
            return tofetch.toString();
        }

    }

}
